﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Collections;

namespace INSAForManagers
{
    public partial class frmCareerInput : Form
    {
        private string file = null;
        private double d2009 = 0.0;
        private double d2008 = 0.0;
        private double d2007 = 0.0;

        public frmCareerInput()
        {
            InitializeComponent();
        }

        private void btnFindTarget_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Microsoft Excel files (*.xls)|*.xls";
            dlg.Title = "승진후보자(연수대상자) 파일";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.file = dlg.FileName;
                Util.openXls(this.file);                

                //소속
                Excel.Range range = Util.OSheet.get_Range("B4", Missing.Value);
                string organ = Convert.ToString(range.get_Value(Missing.Value));

                //직위
                range = Util.OSheet.get_Range("F4", Missing.Value);
                string level = Convert.ToString(range.get_Value(Missing.Value));

                //성명
                range = Util.OSheet.get_Range("H4", Missing.Value);
                string name = Convert.ToString(range.get_Value(Missing.Value));
                name = name.Substring(0, name.IndexOf("(")).Trim();

                if (organ.Equals("") || level.Equals("") || name.Equals(""))
                {
                    MessageBox.Show("해당 파일에 사용자 정보가 없습니다.   ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.file = null;
                    Util.closeXls();
                    return;
                }

                lblTarget.Text = name + "(" + organ + " " + level + ")";

                Util.closeXls();

            }
        }

        private void frmCareerInput_Load(object sender, EventArgs e)
        {
            Hashtable hash = DBUtil.getEvalueList(1, "2010-01-31");
            
            d2009 = Double.Parse(hash["RATIO1"].ToString()) * (double)0.01;
            d2008 = Double.Parse(hash["RATIO2"].ToString()) * (double)0.01;
            d2007 = Double.Parse(hash["RATIO3"].ToString()) * (double)0.01;

            lbl2009.Text = "×" + d2009.ToString("0.00");
            lbl2008.Text = "×" + d2008.ToString("0.00");
            lbl2007.Text = "×" + d2007.ToString("0.00");

            p2007.Text = Double.Parse(p2007.Text).ToString("#0.000");
            p2008.Text = Double.Parse(p2008.Text).ToString("#0.000");
            p2009.Text = Double.Parse(p2009.Text).ToString("#0.000");
            pEvalue.Text = Double.Parse(pEvalue.Text).ToString("#0.000");

        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            double v2007 = Convert.ToDouble(p2007.Text);
            double v2008 = Convert.ToDouble(p2008.Text);
            double v2009 = Convert.ToDouble(p2009.Text);

            if (v2007 == 0.0 || v2008 == 0 || v2009 == 0.0)
            {
                MessageBox.Show("각 연도의 근무성적/합산점을 입력하세요!   ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            double vEvalue = v2007 * d2007 + v2008 * d2008 + v2009 * d2009;

            pEvalue.Text = vEvalue.ToString("#0.000");

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void p2007_Click(object sender, EventArgs e)
        {
            p2007.SelectAll();
        }

        private void p2008_Click(object sender, EventArgs e)
        {
            p2008.SelectAll();
        }

        private void p2009_Click(object sender, EventArgs e)
        {
            p2009.SelectAll();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Double.Parse(pEvalue.Text) == 0.0)
            {
                MessageBox.Show("평정점이 0.0 입니다.   ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (this.file == null)
            {
                MessageBox.Show("대상자를 먼저 선택하세요!   ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {

                Util.openXls(this.file);

                Excel.Range range = Util.OSheet.get_Range("B9", Missing.Value);
                range.set_Value(Missing.Value, (double.Parse(p2007.Text) * d2007).ToString("#0.000"));

                range = Util.OSheet.get_Range("C9", Missing.Value);
                range.set_Value(Missing.Value, (double.Parse(p2008.Text) * d2008).ToString("#0.000"));

                range = Util.OSheet.get_Range("D9", Missing.Value);
                range.set_Value(Missing.Value, (double.Parse(p2009.Text) * d2009).ToString("#0.000"));

                range = Util.OSheet.get_Range("B10", Missing.Value);
                range.set_Value(Missing.Value, pEvalue.Text);

                range = Util.OSheet.get_Range("K9", Missing.Value);
                double total = Double.Parse(range.get_Value(Missing.Value).ToString());

                total += Double.Parse(pEvalue.Text);

                range.set_Value(Missing.Value, total);

                Util.OWorkBook.SaveAs(this.file, Missing.Value, Missing.Value, Missing.Value, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Util.closeXls();

                MessageBox.Show("근평을 입력하였습니다.", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }
        }
    }
}
